package com.atguigu.yygh.hosp.controller;


import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author zhuo
 * @since 2022-03-07
 */
@Api(tags = "医院设置接口开发")
//@CrossOrigin
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    // 医院设置锁定和解锁
    @ApiOperation("医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(@PathVariable Long id,
                             @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);

        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        hospitalSetService.updateById(hospitalSet);
        return R.ok();


    }

    //批量删除医院设置
    @ApiOperation("根据id批量删除")
    @DeleteMapping("batchRemove")
    public R batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        hospitalSetService.removeByIds(idList);
        return R.ok();
    }

    @ApiOperation(value = "新增医院设置")
    @PostMapping("saveHospSet")
    public R save(@RequestBody HospitalSet hospitalSet) {
        hospitalSetService.save(hospitalSet);
        return R.ok();
    }

    @ApiOperation(value = "根据ID查询医院设置")
    @GetMapping("getHospSet/{id}")
    public R getById(@PathVariable String id) {

        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok().data("item", hospitalSet);
    }

    @ApiOperation(value = "根据ID修改医院设置")
    @PostMapping("updateHospSet")
    public R updateById(@RequestBody HospitalSet hospitalSet) {
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    @ApiOperation(value = "分页条件医院设置列表")
    @PostMapping("{page}/{limit}")
    public R pageQuery(@PathVariable Long page,
                       @PathVariable Long limit,
                       @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        // 首先创建Page对象  传入当前页码和每页多少数据
        Page<HospitalSet> pageParam = new Page<>(page, limit);

        // 判断当前查询条件是否为空
        if (hospitalSetQueryVo == null) {

            // 然后进行查询
            hospitalSetService.page(pageParam, null);

        } else {
            // 如果不为空
            QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();

            // 然后判断当前查询条件哪个存在
            String hoscode = hospitalSetQueryVo.getHoscode();
            String hosname = hospitalSetQueryVo.getHosname();

            // 如果医院编码不为空
            if (!StringUtils.isEmpty(hoscode)) {
                wrapper.eq("hoscode", hoscode);
            }

            // 如果医院名字不为空
            if (!StringUtils.isEmpty(hosname)) {
                wrapper.like("hosname", hosname);
            }

            // 然后查询数据返回数据
            hospitalSetService.page(pageParam, wrapper);

        }

        // 返回前端的数据
        List<HospitalSet> records = pageParam.getRecords();


        long total = pageParam.getTotal();

        return R.ok().message("成功").data("total", total).data("rows", records);
    }

    // 添加分页方法
    @ApiOperation(value = "分页医院设置列表")
    @GetMapping("{page}/{limit}")
    public R pageList(@PathVariable Long page, @PathVariable Long limit) {
        // 首先创建Page对象  传入当前页码和每页多少数据
        Page<HospitalSet> pageParam = new Page<>(page, limit);

        // 然后进行查询
        hospitalSetService.page(pageParam, null);
        List<HospitalSet> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        // 返回数据
        return R.ok().data("total", total).data("rows", records);
    }

    // 删除指定id值的医院设置信息
    //@ApiOperation("删除指定id值的医院设置")
    //@DeleteMapping("remove/{id}")
    //public boolean removeById(@PathVariable String id) {
    //    return hospitalSetService.removeById(id);
    //}

    // 删除指定id值的医院设置信息
    @ApiOperation("删除指定id值的医院设置")
    @DeleteMapping("remove/{id}")
    public R removeById(@PathVariable String id) {
        boolean isSuccess = hospitalSetService.removeById(id);

        if (isSuccess) {
            // 成功
            return R.ok().code(20000).message("成功");
        } else {
            // 失败
            return R.error().code(20001).message("失败");
        }
    }

    // 查询所有的HospitalSet医院设置信息
    //@ApiOperation("查询所有的医院设置信息")
    //@GetMapping("findAll")
    //public List<HospitalSet> findAll() {
    //    return hospitalSetService.list();
    //}

    @ApiOperation("查询所有的医院设置信息")
    @GetMapping("findAll")
    public R findAll() {
        List<HospitalSet> list = hospitalSetService.list();
        try {
            int i = 10 / 0;
        } catch (Exception e) {
            throw new YyghException(20001, "自定义算数异常哦");
        }
        return R.ok().code(20000).message("成功").data("list", list);
    }

}

